<?php
/**
 * RUBIK IT Solution & Development ltd.
 *
 * PROJECT		: Rubik Ultimate
 * @version 	: 1.1
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * @author			hoadn <hoadn@rubik.com.vn>
 * @access			[public/private]
 * @package		 [package name]
 *
 * Created on	:Sep 19, 2008
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls('html.fieldInfo.AbstractHtmlFieldInfo');

class SqlMultipleComboboxHtmlFieldInfo extends AbstractHtmlFieldInfo {

	var $_sql = null;
	var $_emptyText = '- - Select please - -';
	var $_defaultValue = "";
	
	/**
	 * Constructor
	 *
	 * @param string $basicName
	 */
	function __construct($basicName, $linkRule = null) {
		parent::__construct($basicName, $linkRule);
	}
	
	/**
	 * Set Prefix for Mutilanguage
	 *
	 * @param str $prefix
	 */
	function setNamePrefix($prefix) {
		$this->_fieldName = $prefix . $this->_fieldName;
	}
	
	function setSQL($sql) {
		$this->_sql = $sql;
	}
	
	function setEmptyText($text=null) {
		$this->_emptyText = $text;
	}
	
	/**
	 * Function for geting the html for inputing data
	 *
	 * @abstract 
	 * @return string
	 */
	function getHtmlInputData($dataType = '', $extAttribute = '') {
		$extAttribute .= $this->buildValidateHtmlAttribute($dataType);
		return SqlMultipleComboboxHtmlFieldInfo::createMultipleCombobox($this->_emptyText, null, null, null, $extAttribute);
	}
	
	/**
	 * Enter description here...
	 *
	 * @param String $emptyText
	 * @param String $cssCls
	 * @param String $style
	 * @param Int $id
	 * @param String $extAttributes
	 */
	function createMultipleCombobox($emptyText = "- - Select Filter - -", $cssCls = null, $style = null, $id = null, $extAttributes = ""){
		$oResult = SqlMultipleComboboxHtmlFieldInfo::getData();

		$aVar = array_keys(get_object_vars($oResult[0]));
		$key1 = $aVar[0];
		$key2 = $aVar[1];
		
		$valueDefault = array();
		if(!empty($this->_defaultValue)){
			$valueDefault = $this->_defaultValue;
			RubikHelper::debugPrePrint($this->_defaultValue);
		}
		
		if (! empty ( $cssCls )) {
			$extAttributes .= "class=\"$cssCls\" ";
		}
		if (! empty ( $style )) {
			$extAttributes .= "style=\"$style\" ";
		}
		
		$result = "<select name=\"$this->_fieldName\" id=\"$this->_fieldName\" $extAttributes multiple=\"multiple\">\n";
		foreach ( $oResult as $row) {
			$key = $row->$key1;
			$value = $row->$key2;
			
			if (in_array($key, $valueDefault )) {
				$result .= "\t<option value=\"$key\" selected=\"true\">$value</option>\n";
			} else {
				$result .= "\t<option value=\"$key\">$value</option>\n";
			}
		}
		$result .= "</select>";
		return $result;
	}
	
	function getData(){
		return SysDatabase::loadObjectList($this->_sql);
	}
	
	/**
	 * Set the value
	 *
	 */
	function setValue($valueOrArrayContain = null) {
		$this->_defaultValue = $valueOrArrayContain;
	}
	
}
?>